﻿//-----------------------------------------------------------------------
// <copyright file="XmlReadHelper.cs" company="AFS">
// Copyright (c) AFS. All rights reserved.
//
//    This file is part of Chatbox.
//
//    Chatbox is free software: you can redistribute it and/or modify
//    it under the terms of the GNU Lesser General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    any later version.
//
//    Chatbox is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//    GNU Lesser General Public License for more details.
//
//    You should have received a copy of the GNU Lesser General Public License
//    along with Chatbox. If not, see http://www.gnu.org/copyleft/lesser.html
// </copyright>
//----------------------------------------------------------------------- 
namespace ChatBox.DomainObjects.XmlFeedObjects
{
    using System;
    using System.Xml.Linq;

    using ChatBox.Utility;

    /// <summary>
    /// Helper methods that go across varios xml feed objects
    /// </summary>
    public static class XmlReadHelper
    {
        /// <summary>
        /// Convert a string to a group type
        /// </summary>
        /// <param name="value">the value to convert</param>
        /// <returns>the group type</returns>
        public static GroupType ToGroupType(this string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                return GroupType.Unknown;
            }

            return (GroupType)Enum.Parse(typeof(GroupType), value, true);
        }

        /// <summary>
        /// Read a chat link from an XML element
        /// </summary>
        /// <param name="element">the xml element to read</param>
        /// <returns>the link read from xml</returns>
        public static Link ToLink(this XElement element)
        {
            if (element == null)
            {
                return null;
            }

            Link result = new Link();
            result.NodeId = element.Attribute("node_id").Value.ToInteger();
            result.Text = element.Value;

            XAttribute typeAttribute = element.Attribute("type");
            if (typeAttribute != null)
            {
                result.GroupType = typeAttribute.Value.ToGroupType();
            }
        
            return result;
        }

        /// <summary>
        /// Read a group link from an XML element
        /// </summary>
        /// <param name="element">the xml element to read</param>
        /// <returns>the link read from xml</returns>
        public static Link ToGroupLink(this XElement element)
        {
            Link result = element.ToLink();
            result.GroupType = GroupType.UserGroup;

            XElement parent = element.Parent;
            if (parent != null && parent.Name == "grp")
            {
                XAttribute groupTypeAttr = parent.Attribute("type");
                if (groupTypeAttr != null && groupTypeAttr.Value == "user") 
                {
                    result.GroupType = GroupType.User;
                }
            }

            return result;
        }
    }
}
